package com.yanqu.road.dao.impl.activity.zhenhuan.thirdProcess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.CrossZhenHuanThirdProcessBanquetData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossZhenHuanThirdProcessBanquetDataDaoImpl extends TempDataObjectDao<CrossZhenHuanThirdProcessBanquetData> {

    public static String getDataSql = "select * from `t_c_zhenhuan_third_process_banquet_data` where `activityId` = ? order by banquetId asc";
    public List<CrossZhenHuanThirdProcessBanquetData> getCrossZhenHuanThirdProcessBanquetDataList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossZhenHuanThirdProcessBanquetData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanThirdProcessBanquetData data = parseData(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String insertSql = "INSERT INTO `t_c_zhenhuan_third_process_banquet_data`(`activityId`, `userId`, `banquetId`, `score`, `startTime`, `banquetState`, `openItemId`) VALUES (?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(CrossZhenHuanThirdProcessBanquetData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenItemId()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_c_zhenhuan_third_process_banquet_data` SET `score` = ?, `startTime` = ?, `banquetState` = ?, `openItemId` = ? WHERE `activityId` = ? AND `userId` = ? AND `banquetId` = ?";
    @Override
    protected boolean updateData(CrossZhenHuanThirdProcessBanquetData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossZhenHuanThirdProcessBanquetData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanThirdProcessBanquetData data = new CrossZhenHuanThirdProcessBanquetData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setBanquetId(rs.getInt("banquetId"));
        data.setScore(rs.getLong("score"));
        data.setStartTime(rs.getLong("startTime"));
        data.setBanquetState(rs.getInt("banquetState"));
        data.setOpenItemId(rs.getInt("openItemId"));
        return data;
    }
}
